package com.bountytask.service.impl;

import com.bountytask.dao.InformationDao;
import com.bountytask.dao.TaskDao;
import com.bountytask.dao.UserTaskDao;
import com.bountytask.entity.Information;
import com.bountytask.service.InformationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class InformationServiceImpl implements InformationService {
    @Autowired
    private InformationDao informationDao;

    @Autowired
    private TaskDao taskDao;

    @Autowired
    private UserTaskDao userTaskDao;

    //添加消息
    @Transactional
    public long addInformation(int role,int status,long taskId,long userId){
        Information info = new Information();
        info.setRole(role);
        info.setCreateTime(new Date());
        info.setUserId(userId);
        info.setTaskId(taskId);
        info.setStatus(status);
        if (role==1) {
            info.setFinishNum(taskDao.getTaskById(taskId).getFinishNum());
            info.setGetNum(taskDao.getTaskById(taskId).getGetNum());
        }
        else {
            info.setGetNum(null);
            info.setFinishNum(null);
        }
        informationDao.addInfo(info);
        return info.getUserId();
    }

    //获得该用户在该角色下的所有消息
    public List<Information> getInfo(int role,long userId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("role",role);
        map.put("userId",userId);
        return informationDao.getInfo(map);
    }

    //设置为已查看
    public Information setViewed(long id){
        if (informationDao.Viewed(id)!=1)
            return null;
        return informationDao.getInfoById(id);
    }

    //批量删除消息
    public void mDeleteInfo(List<Long> ids){
         informationDao.mDelete(ids);
    }

    //从数据库中获得id数组，用于检验前端传的需要删除的id是否正确
    public List<Long> getIds(List<Long> ids){
        return informationDao.getIds(ids);
    }

}
